//
//  AXFSuperMarketController.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/22.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import SnapKit
import YYModel
private let cellId = "cellId"
class AXFSuperMarketController:UIViewController,UITableViewDataSource {
    
    //MARK: 属性
    var dataSource:[AXFSuperModel]?
    
    //左右两个tableview
    let leftTableView = UITableView.init()
    let rightTbleView = UITableView.init()
    
    func loadData(finished:@escaping ([AXFSuperModel])->()){
        //http://iosapi.itcast.cn/loveBeen/supermarket.json.php
        //参数 call 5
        let urlString = "http://iosapi.itcast.cn/loveBeen/supermarket.json.php"
        let parameters = ["call":"5"]
        YMSNetWorkToll.shared.request(method: .POST, urlString: urlString, parameters: parameters) { (responed, error) in
            if(error != nil){
                return;
            }
            var dict = responed as! [String:Any]
            var data:[String:Any] = dict["data"] as! [String : Any]
            let array:[Any] = data["categories"] as! [Any]
            
            var tempArray = [AXFSuperModel]()
            for item in array{
                let model = AXFSuperModel()
                model.yy_modelSet(withJSON: item)
                tempArray.append(model)
            }
            finished(tempArray)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        //搭建 UI
        self .setUI()
        //加载数据
        self.loadData { (array) in
            //赋值
            OperationQueue.main.addOperation {
                self.dataSource = array
                self.leftTableView.reloadData()
            }
        }
    }
    
    //MARK: 搭建 UI 界面
    func setUI(){
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //添加 tableview 左边、右边
        self.view.addSubview(self.leftTableView)
        self.view.addSubview(rightTbleView)
        self.leftTableView.snp.makeConstraints { (make) in
            make.leading.bottom.top.equalTo(self.view)
            make.width.equalTo(100)
        }
        self.rightTbleView.snp.makeConstraints { (make) in
            make.leading.equalTo(self.leftTableView.snp.trailing)
            make.trailing.bottom.top.equalTo(self.view)
        }
        
        self.leftTableView.register(UITableViewCell.self , forCellReuseIdentifier: cellId)
        self.leftTableView.dataSource = self
        
    }
    
    //----------------------dataSource----------------------------------
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource?.count ?? 0
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        let model = self.dataSource![indexPath.row]
        cell.textLabel?.text = model.name
        let view = UIView.init()
        view.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
        cell.selectedBackgroundView = view
        return cell
    }
}


